package com.summer.mall.controller;

import com.summer.mall.common.Const;
import com.summer.mall.common.ResponseCode;
import com.summer.mall.common.ResponseService;
import com.summer.mall.dto.cart.CartAddAndUpdateDTO;
import com.summer.mall.dto.cart.CartDTO;
import com.summer.mall.entity.User;
import com.summer.mall.service.CartService;
import com.summer.mall.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by complone on 2018/7/18.
 */
@Controller
@RequestMapping(value = "/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @Autowired
    private HttpSession session;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public ResponseService<CartVo> list() {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ResponseService.createByCodeErrorMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return cartService.list(user.getId());
    }

    /**
    * @Author : complone
    * @Desrcption: 添加指定的商品数量
    * @Date  15:42
     **/
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public ResponseService<CartVo> add(@RequestBody CartAddAndUpdateDTO cartAddAndUpdateDTO) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ResponseService.createByCodeErrorMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return cartService.add(user.getId(), cartAddAndUpdateDTO.getProductId(), cartAddAndUpdateDTO.getCount());
    }

    /**
    * @Author : complone
    * @Desrcption: 更新购物车内的商品状态,更新指定商品的数量
    * @Date  15:38
     **/
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public ResponseService<CartVo> update(@RequestBody CartAddAndUpdateDTO cartAddAndUpdateDTO) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ResponseService.createByCodeErrorMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return cartService.update(user.getId(), cartAddAndUpdateDTO.getProductId(), cartAddAndUpdateDTO.getCount());
    }

    /**
    * @Author : complone
    * @Desrcption: 根据商品Id定义商品
    * @Date  15:41
     **/
    @RequestMapping(value = "/delete_product",method = RequestMethod.POST)
    @ResponseBody
    public ResponseService<CartVo> deleteProduct(Integer productId) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ResponseService.createByCodeErrorMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return cartService.deleteProduct(user.getId(), productId);
    }

    /**
    * @Author : complone
    * @Desrcption: 全选购物车内的商品
    * @Date  15:34
     **/

    @RequestMapping(value = "/select_all",method = RequestMethod.POST)
    @ResponseBody
    public ResponseService<CartVo> selectAll() {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ResponseService.createByCodeErrorMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return cartService.selectOrUnSelect(user.getId(), null, Const.Cart.CHECKED);
    }

    /**
    * @Author : complone
    * @Desrcption: 全部取消购物车内的商品
    * @Date  15:35
     **/
    @RequestMapping(value = "/un_select_all",method = RequestMethod.POST)
    @ResponseBody
    public ResponseService<CartVo> unSelectAll() {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ResponseService.createByCodeErrorMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return cartService.selectOrUnSelect(user.getId(), null, Const.Cart.UN_CHECKED);
    }

    /**
    * @Author : complone
    * @Desrcption: 选中购物车内的单个商品
    * @Date  15:35
     **/
    @RequestMapping("/select")
    @ResponseBody
    public ResponseService<CartVo> select(Integer productId) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ResponseService.createByCodeErrorMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return cartService.selectOrUnSelect(user.getId(), productId, Const.Cart.CHECKED);
    }

    /**
    * @Author : complone
    * @Desrcption: 取消单个商品
    * @Date  15:36
     **/
    @RequestMapping("/un_select")
    @ResponseBody
    public ResponseService<CartVo> unSelect(Integer productId) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ResponseService.createByCodeErrorMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return cartService.selectOrUnSelect(user.getId(), productId, Const.Cart.UN_CHECKED);
    }


    /**
    * @Author : complone
    * @Desrcption: 结算购物车内的商品价格
    * @Date  15:36
     **/
    @RequestMapping("/get_cart_product_count")
    @ResponseBody
    public ResponseService<Integer> getCartProductCount() {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ResponseService.createBySuccess(0);
        }
        return cartService.getCartProductCount(user.getId());
    }

}
